package models

import (
	"github.com/astaxie/goredis"
)

const (
	URL_QUEUE  = "url_queue"
	URL_VISIT_SET = "url_visit_set"
)

var (
	client goredis.Client
)

func ConnectRedis(addr string, pwd string)  {
	client.Addr = addr
	client.Password = pwd
}

func PutinQueue(url string)  {
	client.Lpush(URL_QUEUE,[]byte(url))
}

func PopfromQueue()string  {
	res,err := client.Rpop(URL_QUEUE)
	if err!=nil{
		panic(err)
	}
	return string(res)
}

func AddToSet(url string)  {
	//reg := regexp.MustCompile(`(https://movie.douban.com/subject/\d+/).*`)
	//url = string(reg.FindAllStringSubmatch(url,-1)[0][1])
	client.Sadd(URL_VISIT_SET, []byte(url))
}

func IsVisit(url string)bool  {
	//reg := regexp.MustCompile(`(https://movie.douban.com/subject/\d+/).*`)
	//url = string(reg.FindAllStringSubmatch(url,-1)[0][1])
	bIsVisit,err := client.Sismember(URL_VISIT_SET,[]byte(url))
	if err != nil {
		return false
	}
	return bIsVisit
}

func GetQueueLength() int {
	length,err := client.Llen(URL_QUEUE)
	if err!=nil {
		return 0
	}
	return length
}

